package portal.web.servlets;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import portal.entidades.Configuracion;
import portal.web.delegate.IPortalWebDelegate;
import portal.web.delegate.PortalWebDelegate;

/**
 * Handler for the update cell action
 */
@WebServlet("/Configuracion/ConfiguracionUpdate")
public class ConfiguracionUpdate extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * This servlet handles post request from the JEditable and updates company
	 * property that is edited
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Long id = new Long(request.getParameter("id"));
		// int columnId = Integer.parseInt(request.getParameter("columnId"));
		int columnPosition = Integer.parseInt(request
				.getParameter("columnPosition"));
		// int rowId = Integer.parseInt(request.getParameter("rowId"));
		String value = request.getParameter("value");
		// String columnName = request.getParameter("columnName");

		IPortalWebDelegate app = PortalWebDelegate.getInstanace();

		List<Configuracion> configuraciones = app.listar();

		for (Configuracion configuracion : configuraciones) {
			Long _id = configuracion.getId();
			
			if (_id.equals(id)) {
				switch (columnPosition) {
				case 0:
					configuracion.setValor((value));
					break;
				case 1:
					configuracion.setNumeroGrupo(new Long(value));
					break;
				case 2:
					configuracion.setTipoServicio(value);
					break;
				default:
					break;
				}

				// TODO: Agregar metodo en backend
				app.actualizarConfiguraciones(configuracion);

				response.getWriter().print(value);
				return;
			}
		}
		response.getWriter()
				.print("Error - la configuracion no fue encontrada");
	}

}
